package Derivation;

import Basics.*;

public class SemiNaive 
{
	Program p;
	
	ParseResultsBottomUp parser;
	

	
	public SemiNaive (Rule ... irs)
	{
		this.p = new Program(irs);
		this.parser = new ParseResultsBottomUp(irs);
	}
	
	
	public SemiNaive (Program irs)
	{
		this.p = irs;
		this.parser = new ParseResultsBottomUp(this.p);
	}
	

	
	public Program getP() 
	{
		return p;
	}


	public void setP(Program p) 
	{
		this.p = p;
	}


	
	public ParseResultsBottomUp getParser() 
	{
		return parser;
	}

	

	public void setHelper(ParseResultsBottomUp iparser)
	{
		this.parser = iparser;
	}


	/*************************************************************************************************************/
	/** Title: SemiNaiveIteration																				
	/** Description: performs one iteration of semi-naive algorithm 			
	/*************************************************************************************************************/
	
	public boolean SemiNaiveIteration (boolean debug, boolean forIntersection)
	{
		boolean retVal = parser.ParseResults(forIntersection);
		long startTime = System.currentTimeMillis();
		if (false == forIntersection) 
		{
			parser.UpdateDB();
		}
		long end = System.currentTimeMillis();
		if ((end-startTime) > 100) 
		{
			System.out.println("SemiNaiveIteration:: time to update DB: " + (end-startTime));
		}
		//DEBUG
		if (true == debug) 
		{
			System.out.println("atoms added at this step: " + parser.getAddedInstAtoms());
			System.out.println("provenance at this step: " + parser.getC().getCircuit());
		}
		
		
		
		parser.Reset();
		return retVal;
	}
	
	
	
	
	/*************************************************************************************************************/
	/** Title: Run																			
	/** Description: SemiNaiveAlgorithm 			
	/*************************************************************************************************************/
	
	public void Run (boolean debug, boolean forIntersection)
	{
		while (false == SemiNaiveIteration(debug, forIntersection));
		
		this.parser.programDeriver.addedInstAtomsInLastIteration.clear();
		this.parser.getC().SanityCheck();
	}
	
}
